package com.qm.struts.friendspace;

import java.util.List;

import org.hibernate.Session;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.qm.hibernate.TblogDAO;
import com.qm.hibernate.TfriendsDAO;
import com.qm.hibernate.TfriendsId;
import com.qm.hibernate.Tpost;
import com.qm.hibernate.TpostDAO;
import com.qm.hibernate.Tuser;
import com.qm.hibernate.TuserDAO;

public class HisIndexAction implements Action{

	private Integer id;
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
	public String add()
	{
		ActionContext ctx = ActionContext.getContext();	
		Tuser user = (Tuser)ctx.getSession().get("user");
		Tuser friend = (Tuser)ctx.getSession().get("friend");
		TfriendsDAO fd = new TfriendsDAO();
		fd.addRelation(user.getId(),friend.getId());
		ctx.getSession().put("isFriends", true);
		return SUCCESS;
	}
	public String del()
	{
		ActionContext ctx = ActionContext.getContext();	
		Tuser user = (Tuser)ctx.getSession().get("user");
		Tuser friend = (Tuser)ctx.getSession().get("friend");
		TfriendsDAO fd = new TfriendsDAO();
		fd.delRelation(user.getId(),friend.getId());
		ctx.getSession().put("isFriends", false);
		return SUCCESS;
	}
	@Override
	public String execute() throws Exception {
		TuserDAO ud = new TuserDAO();
		ActionContext ctx = ActionContext.getContext();	
		Tuser user = (Tuser)ctx.getSession().get("user");
		
		if (getId().equals(user.getId())) return ERROR;
		Tuser friend = ud.findById(getId());
		TpostDAO pd = new TpostDAO();
		List l = pd.findByProperty("tuser.id", getId());
		
		ctx.getSession().put("hisPosts", l);
		ctx.getSession().put("friend", friend);
		TfriendsDAO fd = new TfriendsDAO();
		
		boolean flag = fd.isHaveRelation(getId());
		ctx.getSession().put("isFriends",flag);

		return SUCCESS;
	}
}
